import re
import urllib.request as ur
import user_agent


request = ur.Request(
    url='http://nmggwy.chinahrt.com/',
    headers={
        'User-Agent': user_agent.get_user_agent_pc(),
    }
)

response = ur.urlopen(request).read().decode('utf-8')
# print(response)

with open('nmggwy.html', 'w', encoding='utf-8') as f:
    f.write(response)

with open('nmggwy.html', 'r', encoding='utf-8') as f_read:
    nmggwy_html = f_read.read()

    # 用空格替换换行符
    nmg = re.sub('\n', '',nmggwy_html)

    pattern1 = '<span>(.*?)</span>'
    # 正则匹配结果，返回列表
    ret1 = re.findall(pattern1,nmg)
    print('ret1=', ret1)
    print(ret1[0].strip())

# 正则匹配，前面加r防转义
password_pattren = r'^[a-zA-Z][a-zA-Z0-9_]{5,15}$'

password1 = 'djosid'
password2 = '_09080fhf'
password3 = '1233_jsh'

print(re.match(password_pattren, password1))
print(re.match(password_pattren, password2))
print(re.match(password_pattren, password3))
